// packageD/pages/collect/collect.js
const app = getApp()
const font = require('../../../lib/js/font.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height,
    titleBarHeight: app.globalData.titleBarHeight,
    windowHeight: app.globalData.windowHeight,
    navHeight: app.globalData.navHeight - 2,
    // 组件所需的参数
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '我的收藏', //导航栏 中间的标题
      color: '#FFFFFF'
    },
    token: "",
    pageNum: 1,
    pageSize: 10,
    isSending: false, // 是否正在加载
    isGetMore: true, // 是否还有更多
    noData: false,
    list: [],
    showNo: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.getLists();
    font.loadFont(); //下载字体
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ""
    that.setData({
      token: token
    })
    if (!that.data.token) {
      wx.showToast({
        title: '请先登录',
        icon: 'none',
        duration: 2000
      })
      setTimeout(function(){
        wx.navigateTo({
          url: '/packageD/pages/authorize/authorize'
        })
      },1500)
   
      return
    } 
    that.setData({
      pageNum: 1,
      pageSize: 10,
      isSending: false, // 是否正在加载
      isGetMore: true, // 是否还有更多
      list: [],
      noData: false,
    })
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let that = this;
    that.setData({
      pageNum: 1,
      pageSize: 10,
      isSending: false, // 是否正在加载
      isGetMore: true, // 是否还有更多
      list: [],
      noData: false,
    })
    that.getList();
    wx.stopPullDownRefresh(); //为停止当前页面下拉刷新
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    that.getLists()
  },
  getList() {
    let that = this;
    let param = {
      pageNum: that.data.pageNum,
      pageSize: that.data.pageSize,
    };
    if (that.data.isSending || !that.data.isGetMore) {
      return false;
    }
    that.setData({
      isSending: true,
    })
      app.fly.post('/ma/box/list/collected', param)
      .then(function (res) {

        if (res.code === 0) {
        
          that.data.pageNum++;
          that.setData({
            list: that.data.list.concat(res.data.list),
            pageNum: that.data.pageNum,
          })
          if (that.data.list.length >= res.data.total) {
            that.data.isGetMore = false;
          }
          that.data.isSending = false;
          that.setData({
            pageNum: that.data.pageNum,
            isSending: that.data.isSending,
            isGetMore: that.data.isGetMore,
          })
          if(that.data.list.length<=0){
            that.setData({
              noData: true,
              isGetMore: true
            })
          }else{
            that.setData({
              noData: false,
              isGetMore: false
            })
          }
        } else {
          that.data.isSending = false;
          wx.showToast({
            title: '失败！联系客服',
            icon: 'none',
            duration: 2000
          })
        }
      }, function (res) {
        console.log(res);
      });

  },
  goDetail(e) {
    wx.navigateTo({
      url: '/packageA/pages/detail/detail?id=' + e.currentTarget.dataset.id
    })
  },
  //取消收藏
  delColl(e) {
    let that = this;
      app.fly.post('/ma/box/collect', {
        boxId: e.currentTarget.dataset.id,
        status: 0,
      })
        .then(function (res) {
          if (res.code === 0) {
            that.data.list.splice(e.currentTarget.dataset.index,1)
            that.setData({
              list: that.data.list
            })
            if(that.data.list.length<=0){
              that.setData({
                noData: true,
                isGetMore: true
              })
            }else{
              that.setData({
                noData: false
              })
            }
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 2000
            });
          }
        })
  },
})